/**
 * File FileUtils.java
 * ---------------------------------------------------------
 *
 * Copyright (C) 2012 David Bauske (david.bauske@googlemail.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
 * following conditions:
 *
 * - The above copyright notice and this permission notice shall be included in all copies or substantial portions of
 * the Software.
 * - The origin of the software must not be misrepresented.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 * FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * TL;DR: As long as you clearly give me credit for this software, you are free to use as you like, even in commercial
 * software, but don't blame me if it breaks something.
 */

package net.cl.util.files;

import java.io.File;

/**
 * Contains several static helper methods related to files.
 * @author David Bauske
 */
public final class FileUtils
{
	/**
	 * Computes the base path of a {@link File} (the path to the directory where the file is located in).
	 *
	 * <h3>Example:</h3>
	 * <p>
	 * Let <tt>/home/me/some/directory/file.txt</tt> be an input file. Then the output of the method will be a string
	 * similar to this: <tt>/home/me/some/directory/</tt> (Note the trailing directory seperator)
	 * </p>
	 *
	 * @param pInput The input file. May represent a file or a directory.
	 * @return A string representing the path the the directory where pInput is located in.
	 *         Will always be an absolute path. Never returns null.
	 * @throws NullPointerException if pInput is null.
	 */
	public static String getBasePathAsString(File pInput)
	{
		String srcFilename = pInput.getAbsolutePath();
		String basePath = srcFilename.substring(0,
				srcFilename.indexOf(pInput.getName()));

		return basePath;
	}

	/**
	 * Similar to {@link #getBasePathAsString(File)} except that the result is of type {@link File}.
	 */
	public static File getBasePathAsFile(File pInput)
	{
		return new File(getBasePathAsString(pInput));
	}

	/**
	 * Returns the Extension of a given file. As this method is concerned, the file extension is the all that comes
	 * after the last dot in the filename. Does not check for file existence, if the file doesn't exist
	 * the extension will properly be returned.
	 *
	 * <h3>Examples:</h3>
	 *
	 * <pre>
	 * {@code
	 * getFileExtension(new File("/my/dir/file.txt")); // returns "txt"
	 * getFileExtension(new File("/my/dir/file.BMP")); // returns "BMP"
	 * getFileExtension(new File("/my/dir/")); // returns ""
	 * }
	 * </pre>
	 *
	 * @param pInput The input file. May not be null.
	 * @return A string containing the file extension of the file or the empty string if (a) pInput is not a file or (b)
	 *         pInput does not feature a file extension.
	 * @throws NullPointerException If pInput is null.
	 */
	public static String getFileExtension(File pInput)
	{
		int dotPos = pInput.getName().lastIndexOf('.');
		if(dotPos >= 0)
		{
			return pInput.getName().substring(dotPos + 1);
		}
		else
		{
			return "";
		}
	}

	/**
	 * This class contains static methods only, so instantiating it would be pointless.
	 */
	private FileUtils()
	{
	}
}
